package cinema;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;


/**
 *
 * @author Flávia Veloso Queiroz
 */
public class Operacoes {
 
    //declara apenas metodos e atributos estaticos
    //O acesso à The Movie Database API é feito exclusivamente pelos métodos da classe Operacoes
    
    public static Episodio[] autoRepresentacao(String ator, String serie) throws MalformedURLException, IOException
    {
        
        Ator at = Colecoes.findAtor(ator);
        
        Serie sr = Colecoes.findSerie(serie);
        
        Scanner scanner;
        
        ArrayList <Episodio> episodios = new ArrayList<>(); 
       // Episodio[] ap = null;
       // episodios.toArray(ap);
        
        if(at == null)
        {
            URL url = new URL("http://api.themoviedb.org/3/search/person?api_key="
                    + "2c179255fe73cce70fbf10d27856228d&query=" + ator.replaceAll(" ", "%20"));
           
             
            scanner = new Scanner(url.openStream());
            String infoAtor = new String();
            
            while (scanner.hasNext())
            {
                infoAtor += scanner.nextLine();
            }

            scanner.close();
            
            int idAtor = 0;
           
            idAtor = Integer.parseInt(infoAtor.substring(infoAtor.indexOf("id\":")+ 4, 
                    infoAtor.indexOf(",", infoAtor.indexOf("id\":")+ 4))); 
            
            System.out.println("\n" + idAtor);
            
            at = new Ator(idAtor);           
            
        }
        
        if(sr == null)
        {
            URL url = new URL("http://api.themoviedb.org/3/search/tv?api_key="
                    + "2c179255fe73cce70fbf10d27856228d&query="+ serie.replaceAll(" ", "%20"));
            
            scanner = new Scanner(url.openStream());
            String infoSerie = new String();
            
            while (scanner.hasNext())
            {
                infoSerie += scanner.nextLine();
            }

            scanner.close();
            
            int idSerie = 0;
           
            idSerie = Integer.parseInt(infoSerie.substring(infoSerie.indexOf("id\":")+ 4, 
                    infoSerie.indexOf(",", infoSerie.indexOf("id\":")+ 4))); 
            
            System.out.println("\n" + idSerie);
            
            sr = new Serie(idSerie);          
            
        }
        
        
        
        return null;     
       
    }
    
    public static Filme[] filmesEmComum(String ator1, String ator2, int decada) throws MalformedURLException, IOException
    {
        
        
        
        return null;
    }
    
    public static String[] generosDeFilmes(String ator) throws MalformedURLException, IOException
    {
          
        URL url = new URL("http://api.themoviedb.org/3/search/person?api_key="
                    + "2c179255fe73cce70fbf10d27856228d&query=" + ator.replaceAll(" ", "%20"));           
         
             
            Scanner scanner = new Scanner(url.openStream());
            String infoAtor = new String();
            
            while (scanner.hasNext())
            {
                infoAtor += scanner.nextLine();
            }

            scanner.close();
            
            int idAtor = 0;
           
            idAtor = Integer.parseInt(infoAtor.substring(infoAtor.indexOf("id\":")+ 4, 
                    infoAtor.indexOf(",", infoAtor.indexOf("id\":")+ 4))); 
            
            System.out.println("\n" + idAtor);
            
            Ator at = new Ator(idAtor);        
        
            
        
        return null;
        /*
         * O primeiro é a consulta à lista de filmes que tiveram um determinado ator no elenco: 
            http://api.themoviedb.org/3/person/<person_id>/movie_credits?api_key=<key>
         */        
                
    }
}
